<?php

namespace App\Controller\App;

use App\Base\BaseController4App;
use App\Model\GradeCategoryModel;
use App\Model\SubjectModel;
use App\Model\TeacherModel;
use App\Model\TopicModel;
use App\Model\VideoModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

/**
 * @route("/app")
 */
class TopicController extends BaseController4App
{

    /**
     * @route("/getTopic",name="AppGetTopicList")
     */
    public function AppGetTopicList()
    {
        if (!$grade = $this->getParamRequired("grade")) {return $this->json_out(0,"");}
        if (!$subject = $this->getParamRequired("subject")) {$subject = 0;}
        if (!$page = $this->getParamRequired("page")) {$page=0;}
        $this->data;
        //获取列表
        $TopicModel = new TopicModel($this->getDoctrine());
        $data = $TopicModel->getIndexList($grade,$subject,$page,10);
        $this->data = $data;
        return $this->json_out(1,"OK",$this->data);
    }

    /**
     * @route("/getTopicInfo",name="AppGetTopicInfo")
     */
    public function AppGetTopicInfo()
    {
        $uid = $this->needToBeUser();
        if (!$id = $this->getParamRequired("id")) {return $this->json_out(0,"参数错误");}
        $TopicModel = new TopicModel($this->getDoctrine());
        $VideoModel = new VideoModel($this->getDoctrine());
        $TeacherModel = new TeacherModel($this->getDoctrine());

        $topic = $TopicModel->getById($id);
        if (!$topic) {
            return $this->json_out(404,"请求的内容不存在");
        }
        $teacher = $TeacherModel->getById($topic->getTeacherId());
        $this->data["teacher"] = $this->toArray($teacher) ;
        $this->data["topic"] = $this->toArray($topic) ;
        $videos = $VideoModel->getList4Topic($id);
        foreach ($videos as &$one) {
            $one["miniTitle"] = "课程".$this->toChineseNumerals($one["orders"]);
            //获取一下用户的播放权限
            //todo 等用户登录做完再补充
            $one["playType"] = 4;//1=免费，2=试听，3=需要充值VIP，4=正常播放
        }

        $this->data["list"] = $videos;
        return $this->json_out(1,"OK $uid",$this->data);
    }

    /**
     * 数字格式化成汉字
     *
     * @param number $figure 待格式化的数字
     * @param boolean $capital 使用汉字大写数字
     * @param boolean $mode 单字符转换模式
     * @return string
     */
    public function toChineseNumerals($figure, $capital = false, $mode = true)
    {

        $numberChar = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
        $unitChar = ['', '十', '百', '千', '', '万', '亿', '兆', '京', '垓', '秭', '穣', '沟', '涧', '正', '载', '极', '恒河沙', '阿僧祇', '那由他', '不可思议', '无量大数'];
        if ($capital !== false) {
            $numberChar = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
            $unitChar = ['', '拾', '佰', '仟', '', '万', '亿', '兆', '京', '垓', '秭', '穣', '沟', '涧', '正', '载', '极', '恒河沙', '阿僧祇', '那由他', '不可思议', '无量大数'];
        }


        $dec = "点";
        $target = '';
        $matches = [];

        if ($mode) {
            preg_match("/^0*(\d*)\.?(\d*)/", $figure, $matches);
        } else {
            preg_match("/(\d*)\.?(\d*)/", $figure, $matches);
        }

        list(, $number, $point) = $matches;

        if ($point) {
            $target = $dec . self::toChineseNumerals($point, $capital, false);
        }

        if (!$number) {
            return $target;
        }

        $str = strrev($number);
        for ($i = 0; $i < strlen($str); $i++) {
            $out[$i] = $numberChar[$str[$i]];
            if ($mode === false) {
                continue;
            }
            $out[$i] .= $str[$i] != '0' ? $unitChar[$i % 4] : '';
            if ($i > 0 && $str[$i] + $str[$i - 1] == 0) {
                $out[$i] = '';
            }
            if ($i % 4 == 0) {
                $temp = substr($str, $i, 4);
                $out[$i] = str_replace($numberChar[0], '', $out[$i]);
                if (strrev($temp) > 0) {
                    $out[$i] .= $unitChar[4 + floor($i / 4)];
                } else {
                    $out[$i] .= $numberChar[0];
                }
            }
        }
        return join('', array_reverse($out)) . $target;
    }

}
